<?php

//Set values for page
$page_title = "Cart";
$current_page = "cart";
require_once 'global.inc.php';
$pass = DB_PASSWORD;

if (isset($_REQUEST['action'])) {

	$action = $_REQUEST['action']; //the action from the URL

	if ((isset($_REQUEST['product']))) {
		$product_id = $_REQUEST['product']; //the product id from the URL
		if($product_id && !$GLOBALS['FE_DB']->productExists($product_id, 'items')) {
			die("Error: . Product Doesn't Exist");
		}
		switch($action) { //decide what to do
			case "add":
				if (!empty($_SESSION['cart'][$product_id])) {
					$_SESSION['cart'][$product_id]++; //add one to the quantity of the product with id $product_id
				} else {
					$_SESSION['cart'][$product_id] = 1;
				}
			break;
			case "remove":
				if (!empty($_SESSION['cart'][$product_id])) {
					unset($_SESSION['cart'][$product_id]);
				}
			break;
		}
	}
	if ($action == 'update') {
		foreach($_SESSION['cart'] as $product_id => $quantity) {
			if (isset($_REQUEST['product'.$product_id])) {
				$newQuantity = $_REQUEST['product'.$product_id];
				if ($newQuantity) {
					if ($newQuantity > 0 && $newQuantity <= 999) {
						$_SESSION['cart'][$product_id] = $newQuantity;
					} else {
						die('Some proudcts not updated!, quantity must be a number between 1 and 999');
					}
				} else { // $newQuantity = 0
					unset($_SESSION['cart'][$product_id]);
				}
			}
		}
	}
	if ($action =='empty') {
		unset($_SESSION['cart']); //unset the whole cart, i.e. empty the cart.
	}
}
/*if (isset($_SESSION['cart'])) {
	echo '<pre>'.print_r($_SESSION['cart'],TRUE).'</pre>';
}*/
//http://www.sourcecodester.com/php/4220/how-build-shopping-cart-w-checkout-php.html

?>
<!DOCTYPE html>
<html lang="en">
	<?php include 'includes/header.php';?>
	
<body>

	<?php include 'includes/menu.php';?>
	<?php //include 'includes/slideshow.php';?>
	<div class="container" style="margin-top:60px;">
		<?php include 'includes/widget-login.php';?>
		<div class="row">
			<div class="span12">
				<h1>Shopping cart</h1>
				<form name="form1" method="post">
				<input type="hidden" name="action" value=""/>
				<input type="button" value="Continue Shopping" onclick="window.location='product.php'" />
			<?php

				if(!empty($_SESSION['cart'])) { //if the cart isn't empty
				//show the cart
					$connection = mysql_connect('localhost', 'root', $pass);
					mysql_select_db('ecom');
					echo "<table border=\"1\" padding=\"3\" width=\"40%\">"; //format the cart using a HTML table
					$total = '';
					//iterate through the cart, the $product_id is the key and $quantity is the value
					foreach($_SESSION['cart'] as $product_id => $quantity) {
						//get the name, description and price from the database - this will depend on your database implementation.
						//use sprintf to make sure that $product_id is inserted into the query as a number - to prevent SQL injection
						$sql = sprintf("SELECT item_name, description, price FROM items WHERE item_id = %d;", $product_id);
						$result = mysql_query($sql);
						//Only display the row if there is a product (though there should always be as we have already checked)
						if(mysql_num_rows(mysql_query($sql)) > 0) {
							list($name, $description, $price) = mysql_fetch_row($result);
							$line_cost = $price * $quantity; //work out the line cost
							$total = $total + $line_cost; //add to the total cost
							echo "<tr>";
							echo "<td align=\"center\">$name</td>";
							echo "<td align=\"center\"><input type=\"text\" name=\"product$product_id\" value=\"$quantity\" size=\"2\" /> <a href=\"$_SERVER[PHP_SELF]?action=remove&product=$product_id\">X</a></td>";
							echo "<td align=\"center\">\$$line_cost</td>";
							echo "</tr>";
						}
					}
					mysql_close($connection);
					//show the total
					echo "<tr>";
					echo "<td colspan=\"2\" align=\"right\">Total</td>";
					echo "<td align=\"right\">\$$total</td>";
					echo "</tr>";
					echo "<tr>";
					echo "<td align=\"right\"><a href=\"$_SERVER[PHP_SELF]?action=empty\" onclick=\"return confirm('Are you sure?');\">Empty Cart</a></td>";
					echo "<td align=\"right\"><input type=\"button\" value=\"Update Cart\" onclick=\"update_cart()\"></td>";
					echo "<td align=\"right\"><input type=\"button\" value=\"Place Order\" onclick=\"window.location='billing.php'\"></td>";
					echo "</tr>";
					echo "</table>";
				} else {
					//otherwise tell the user they have no items in their cart
					echo "You have no items in your shopping cart.";
				}
				?>
				</form>
			</div>
		</div>
	</div>
	<?php include 'includes/footer.php';?>
	<!-- javascript -->
	<script src="js/jquery.js"></script>
	<script src="js/bootstrap.js"></script>
	<script language="javascript">
	function update_cart(){
		document.form1.action.value='update';
		document.form1.submit();
	}
	</script>
	</body>
</html>